package com.yksj.healthtalk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

public class ProblemDetailAdapter extends BaseAdapter{

	private Context context;
	LayoutInflater inflater;
	String format;
	
	public  ProblemDetailAdapter(Context context){
		this.context = context;
		inflater  = LayoutInflater.from(context);
		format = context.getResources().getString(R.string.problem_support);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 5;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.problem_detail_item, null);
			holder.questionTime = (TextView) convertView.findViewById(R.id.questionTime);
			holder.questionHeader = (ImageView)convertView.findViewById(R.id.questionHeader);
			holder.questionContent = (TextView)convertView.findViewById(R.id.questionContent);
			holder.bestAnswerBtn = (Button) convertView.findViewById(R.id.best_answer_btn);
			holder.supportBtn = (Button) convertView.findViewById(R.id.support_btn);
			convertView.setTag(holder);
		}else {
			holder = 	(ViewHolder) convertView.getTag();
		}
		
		if (position == 0 ) {
			holder.bestAnswerBtn.setVisibility(View.VISIBLE);
			holder.supportBtn.setVisibility(View.GONE);
		}else {
			holder.bestAnswerBtn.setVisibility(View.GONE);
			holder.supportBtn.setVisibility(View.VISIBLE);
		}
		
	    String sFinalAge = String.format(format, 2);  
		
	    holder.supportBtn.setText(sFinalAge);
	    
	    holder.supportBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (v.getTag() == null) {
				    holder.supportBtn.setText(String.format(format, 3)  );
					v.setTag(position);
				}else {
					ToastUtil.showShort(context,"只能支持一次");
				}
			}
		});
		return convertView;
	}

	
	class ViewHolder{
		TextView  questionTime;
		ImageView  questionHeader;
		TextView  questionContent;
		Button bestAnswerBtn;
		Button supportBtn;
	}

}
